// src/user/user.controller.ts
import { Controller, Post, Body } from '@nestjs/common';
import { UserService } from './user.service';
import { ValidationService } from '../common/services/validation.service';
import { CreateUserByEmailDto } from './dto/create-user.dto';
import { User } from './schemas/user.schema';
import { ValidationException } from 'src/common/exceptions/custom-exception';

@Controller('users')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly validationService: ValidationService,
  ) {}

  @Post('create')
  async createByEmail(
    @Body() createUserByEmailDto: CreateUserByEmailDto,
  ): Promise<User> {
    await this.validationService.validateDto(
      CreateUserByEmailDto,
      createUserByEmailDto,
      'userValidateFail',
    );

    const isUserExist = await this.userService.IsusersExist(
      createUserByEmailDto.username,
    );
    if (isUserExist) {
      throw new ValidationException('createUserAlreadyExists');
    }
    const user = await this.userService.createByEmail(createUserByEmailDto);
    return user;
  }

  @Post('login')
  async loginByEmail(@Body() loginInfo: CreateUserByEmailDto) {
    await this.validationService.validateDto(
      CreateUserByEmailDto,
      loginInfo,
      'userValidateFail',
    );
    const user = await this.userService.findByUsername(
      loginInfo.username,
      loginInfo.password,
    );
    if (!user) {
      throw new ValidationException('loginCheckFailInfo');
    }

    return user;
  }

  // @Get(':id')
  // async findById(@Param('id') id: string): Promise<User> {
  //   return this.userService.findById(id);
  // }
}
